package com.property.manage.service.renovation;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guardchina.framework.mybatis.model.CustomPage;
import com.guardchina.framework.util.exception.BusinessException;
import com.guardchina.framework.util.utils.PhoneUtil;
import com.property.manage.dao.renovation.DepositMapper;
import com.property.manage.dao.renovation.InspectionMapper;
import com.property.manage.model.renovation.DepositEntity;
import com.property.manage.model.renovation.DepositVo;
import com.property.manage.model.renovation.Inspection;
import com.property.manage.model.renovation.InspectionVo;
import com.property.manage.model.resource.BuildingVo;
import com.property.manage.model.system.SysSet;
import com.property.manage.service.flowingWater.FlowingWaterService;
import com.property.manage.service.resource.BuildingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * 描述
 *
 * @author lwb
 * @since 2020/7/15
 */
@Component
@Slf4j
public class InspectionService {
    @Autowired
    private InspectionMapper inspectionMapper;
    @Autowired
    private BuildingService buildingService;

    public CustomPage<InspectionVo> page(Long pageNo, Long pageSize, Integer villageId, Integer building, Integer unit, String roomNumber, String startTime, String endTime) {
        Page<InspectionVo> page = new Page<>(pageNo, pageSize);
        IPage<InspectionVo> village = inspectionMapper.getPage(page, villageId, building, unit, roomNumber, startTime, endTime);
        return new CustomPage<>(village);
    }

    public void add(Integer buildingId,
                    String name, LocalDate time, String remarks

    ) {
        Inspection inspection = new Inspection();
        inspection.setBuildingId(buildingId);
        inspection.setAddress(buildingService.findAddress(buildingId));
        inspection.setName(name);
        inspection.setVillageId(buildingService.findBuilding(buildingId).getVillageId());
        inspection.setTime(time);
        inspection.setRemarks(remarks);
        inspectionMapper.insert(inspection);
    }

    public void update(Integer id, Integer buildingId,
                       String name, LocalDate time, String remarks
    ) {
        UpdateWrapper<Inspection> wrapper = new UpdateWrapper<>();
        wrapper.set("building_id", buildingId);
        wrapper.set("name", name);
        wrapper.set("time", time);
        wrapper.set("remarks", remarks);
        wrapper.set("address", buildingService.findAddress(buildingId));
        wrapper.set("village_id", buildingService.findBuilding(buildingId).getVillageId());
        wrapper.eq("id",id);
        inspectionMapper.update(null, wrapper);
    }



    public void delete(Integer id) {
        inspectionMapper.deleteById(id);
    }


}
